#include "configRequest.h"   
size_t configCallback(char *content ,size_t size,size_t nmemb,void *userp)
{
    size_t realsize = size*nmemb;
    char *result = (char *)content;
    cJSON *jsonStr = cJSON_Parse(result);
    if (jsonStr != NULL)
    {
        cJSON * successRoot = cJSON_GetObjectItemCaseSensitive(jsonStr,"success"); 
        cJSON * codeRoot = cJSON_GetObjectItemCaseSensitive(jsonStr,"code");
        cJSON * messageRoot = cJSON_GetObjectItemCaseSensitive(jsonStr,"message");
        if (!cJSON_IsTrue(successRoot))
        {
            printf("%s\n",messageRoot->valuestring);
            return -1;
        }
        //正常去解析指令集
        cJSON *dataRoot = cJSON_GetObjectItemCaseSensitive(jsonStr,"data");
        cJSON *adList = cJSON_GetObjectItemCaseSensitive(dataRoot,"ad_list");
        cJSON *cmdList = cJSON_GetObjectItemCaseSensitive(dataRoot,"cmd_list");
        //cmdList 取出里面的指令去执行
    }
    return realsize;
    
    
}
//发送配置请求
void sendConfigRequest(){
    //1.读取当前文件目录下面的config.ini  获取 devinfo_path 地址
    GKeyFile *configiniPath=g_key_file_new();
    if(!g_key_file_load_from_file(configiniPath,"./config.ini",G_KEY_FILE_NONE,NULL)){
      g_warning("load config.ini error!");
      //return NULL;
    }
    gchar *path =g_key_file_get_string(configiniPath,"config","devinfo_path",NULL);
    g_key_file_free(configiniPath);

    //2.去devinfo_path指定的文件中读取 ip 和 地址
    GKeyFile *configPath = g_key_file_new();
    if(!g_key_file_load_from_file(configPath,path,G_KEY_FILE_NONE,NULL)){
      g_warning("load config.ini error!");
      //return NULL;
    }
    gchar *gatewayIp= g_key_file_get_string(configPath,"config","gatewayIp",NULL);
    gchar *location= g_key_file_get_string(configPath,"config","location",NULL);
    g_key_file_free(configPath);

    char *url ="http://%s/config.json?sn=%s&location=%s";
    char * realUrl= calloc(1,100);
    sprintf(realUrl,url,gatewayIp,location);
    printf("request address:%s\n",realUrl);

    //发送一个get请求
    sendGetRequest(realUrl,configCallback);
}
